package com.lemon.secondhandplatform.ui.activity.good;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Good;
import com.lemon.secondhandplatform.mode.Photo;
import com.lemon.secondhandplatform.ui.activity.BaseActivity;
import com.lemon.secondhandplatform.ui.util.ToastUtils;
import com.lemon.secondhandplatform.util.PathGetter;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 商品添加
 */
public class Add extends BaseActivity {

    private Good good;
    private Photo photo;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tvGoodName)
    TextView tvGoodName;
    @BindView(R.id.imgGoodPhoto)
    ImageView imgGoodPhoto;
    @BindView(R.id.tvGoodIntroduction)
    TextView tvGoodIntroduction;
    @BindView(R.id.tvGoodPrice)
    TextView tvGoodPrice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_add);
        bindService();

        good = new Good();
        initView();
        bindListener();
    }

    private void initView() {
        toolbar.inflateMenu(R.menu.release);
    }

    /**
     * 配置监听事件
     */
    private void bindListener() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    //点击发布商品
                    case R.id.itemRelease:
                        binder.release(good, photo, new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null) {
                                    ToastUtils.show("发布成功");
                                    finish();
                                }
                            }
                        });
                        break;
                }

                return false;
            }
        });
    }

    //请求码：编辑商品名称
    public static final int EDIT_GOOD_NAME = 1;

    /**
     * 编辑商品名称
     */
    @OnClick(R.id.goodName)
    public void editGoodName(View v) {
        String goodName = tvGoodName.getText().toString();
        Intent intent = Edit.newIntent(this, "商品名称", goodName);
        startActivityForResult(intent, EDIT_GOOD_NAME);
    }

    //请求码：打开图库
    public static final int OPEN_GALLERY = 10;

    /**
     * 编辑商品图片
     *
     * @param v
     */
    @OnClick(R.id.goodPhoto)
    public void editGoodPhoto(View v) {
//        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        Intent intent = new Intent();
        //选择图片格式
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, OPEN_GALLERY);
    }

    //请求码：编辑商品介绍
    public static final int EDIT_GOOD_INTRODUCTION = 11;

    /**
     * 编辑商品介绍
     *
     * @param v
     */
    @OnClick(R.id.goodIntroduction)
    public void editGoodIntroduction(View v) {
        String goodName = tvGoodIntroduction.getText().toString();
        Intent intent = Edit.newIntent(this, "商品介绍", goodName, Edit.TEXTAREA);
        startActivityForResult(intent, EDIT_GOOD_INTRODUCTION);
    }

    //请求码：编辑商品价格
    public static final int EDIT_GOOD_PRICE = 100;

    /**
     * 编辑商品价格
     *
     * @param v
     */
    @OnClick(R.id.goodPrice)
    public void editGoodPrice(View v) {
        String price = tvGoodPrice.getText().toString();
        Intent intent = Edit.newIntent(this, "商品价格", price, Edit.NUMBER);
        startActivityForResult(intent, EDIT_GOOD_PRICE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            //编辑商品名称
            case EDIT_GOOD_NAME:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra(Edit.EXTRA_DATA);
                    tvGoodName.setText(result);
                    good.setName(result);
                }
                break;

            //将从相册中选中的图片展示到界面
            case OPEN_GALLERY:
                if (resultCode == RESULT_OK) {
                    Uri uri = data.getData();
                    try {
                        //通过uri获取到bitmap对象
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                        imgGoodPhoto.setImageBitmap(bitmap);

                        //通过uri获取图片路径
                        String path = PathGetter.getPath(this, uri);
                        File file = new File(path);
                        BmobFile bmobFile = new BmobFile(file);
                        photo = new Photo(bmobFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;

            //编辑商品介绍
            case EDIT_GOOD_INTRODUCTION:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra(Edit.EXTRA_DATA);
                    tvGoodIntroduction.setText(result);
                    good.setIntroduction(result);
                }
                break;

            //编辑商品价格
            case EDIT_GOOD_PRICE:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra(Edit.EXTRA_DATA);
                    Double price = Double.valueOf(result.equals("") ? "0" : result);
                    tvGoodPrice.setText(price.toString());
                    good.setPrice(price);
                }
                break;
        }
    }

}
